/* ******************************* *
 *       -=# Array Life #=-        *
 *             =====               *
 *         By: TarpeyD12           *
 * =============================== *
 *        Version: 0.3.5.9         *
 * ******************************* *
 *     Display Functions file.     *
 *        alfunc_display.c         *
 * ******************************* */

#include "array_life.h"


int stats(int stats_x, int stats_y, int dna_size);/**/
int all_stats(int gs_x, int gx_y, int dna_size);/**/
int disp_occp_grid(WINDOW *win, int dispx, int dispy);/**/
int disp_dna_grid();/**/
int acids(int aa_x, int aa_y, int dna_size);

void genstats(void);/**/
void cls(int times);/**/
void clsb(int times);/**/

/* the arrays and grids. */
extern int grid_box[GRIDSIZEX][GRIDSIZEY][DNASIZE];
extern int occp[GRIDSIZEX][GRIDSIZEY];

/* what loop it is. */
extern int loop;


int stats(int stats_x, int stats_y, int dna_size) { /* shows the statistics for given cell. */
	int dna_tmp=0, aa_x=stats_x, aa_y=stats_y;
	printw("\nthe statistics for: grid_box");
	textcolor(6, 7, 5);
	printw("[%2d]", stats_x);
	textcolor(6, 7, 6);
	printw("[%2d]", stats_y);
	textcolor(6, 7, 0);
	printw(":\n");
	printw("occupied: ");
	switch(occp[stats_x][stats_y]){
		case 3:
		case 1: textcolor(BOLD, GREEN, 0); printw("Yes\n"); textcolor(0, 7, 0); break;
		case 0: textcolor(BOLD, RED, 0); printw("No\n"); textcolor(0, 7, 0); break;
	};
	printw("DNA:");
	for(dna_tmp=0;dna_tmp<dna_size;dna_tmp++){
		switch(grid_box[stats_x][stats_y][dna_tmp]){
			case 0: textcolor(DIM, 7, 0); printw("-"); textcolor(6, 7, 0); break;
			case 1: textcolor(BRIGHT, 7, 0); printw("#"); textcolor(6, 7, 0); break;
			/*printf("");*/
		}
		textcolor(6, 7, 0);
	}
	textcolor(6, 7, 0);
	printw("\nAAS:");
	/*acids(stats_x, stats_y, dna_size);*/ /* requires the acids(); function to func. */
	for(dna_tmp=0;dna_tmp<dna_size;dna_tmp++){
		     if(grid_box[aa_x][aa_y][dna_tmp]==0 && grid_box[aa_x][aa_y][(dna_tmp+1)]==0 && grid_box[aa_x][aa_y][(dna_tmp+2)]==0)
			{textcolor(6, 0, 0); printw(" 0 "); textcolor(0, 7, 0); }/* 000 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==0 && grid_box[aa_x][aa_y][(dna_tmp+1)]==0 && grid_box[aa_x][aa_y][(dna_tmp+2)]==1)
			{textcolor(6, 1, 1); printw(" 1 "); textcolor(0, 7, 0); }/* 001 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==0 && grid_box[aa_x][aa_y][(dna_tmp+1)]==1 && grid_box[aa_x][aa_y][(dna_tmp+2)]==0)
			{textcolor(6, 2, 2); printw(" 2 "); textcolor(0, 7, 0); }/* 010 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==1 && grid_box[aa_x][aa_y][(dna_tmp+1)]==0 && grid_box[aa_x][aa_y][(dna_tmp+2)]==0)
			{textcolor(6, 3, 3); printw(" 3 "); textcolor(0, 7, 0); }/* 100 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==0 && grid_box[aa_x][aa_y][(dna_tmp+1)]==1 && grid_box[aa_x][aa_y][(dna_tmp+2)]==1)
			{textcolor(6, 4, 4); printw(" 4 "); textcolor(0, 7, 0); }/* 011 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==1 && grid_box[aa_x][aa_y][(dna_tmp+1)]==0 && grid_box[aa_x][aa_y][(dna_tmp+2)]==1)
			{textcolor(6, 5, 5); printw(" 5 "); textcolor(0, 7, 0); }/* 101 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==1 && grid_box[aa_x][aa_y][(dna_tmp+1)]==1 && grid_box[aa_x][aa_y][(dna_tmp+2)]==0)
			{textcolor(6, 6, 6); printw(" 6 "); textcolor(0, 7, 0); }/* 110 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==1 && grid_box[aa_x][aa_y][(dna_tmp+1)]==1 && grid_box[aa_x][aa_y][(dna_tmp+2)]==1)
			{textcolor(6, 0, 7); printw(" 7 "); textcolor(0, 7, 0); }/* 111 */
		
		dna_tmp++;
		dna_tmp++;
	}
	textcolor(6, 7, 0);
	printw("\nStrength: ");
	textcolor(BRIGHT, RED, BLACK);
	printw("%d", strength(stats_x, stats_y, dna_size));
	textcolor(6, 7, 0);
	/*printw("\n\n");*/
	/* cleans up. */
	free(malloc(stats_x));
	free(malloc(stats_y));
	free(malloc(aa_x));
	free(malloc(aa_y));
	free(malloc(dna_size));
	free(malloc(dna_tmp));
	refresh();
	return 1;
};

int all_stats(int gs_x, int gs_y, int dna_size) {
	int x, y, csr_x=0, csr_y=0, cnt=0, wt_tmp=0, cs_tmp;
	initscr(); /* start ncurses */
	/*move(0, 0);*/
	chk_grid(); /* requires the chk_grid(); to be functioning */
	for(y=0;y<gs_y;y++) {
		for(x=0;x<gs_x;x++) {
			if(occp[x][y]==1) {
				stats(x, y, dna_size); /* requires stats(); to be functioning */
				printw("\n");
				cnt++;
				refresh();
			}

			if(cnt%5==4) {
				wait(1);
				/*printw("\n\nPress [Return] to see 3 more...");*/
				wt_tmp++; cnt++;
				move(0, 0);
				cls(25);
				refresh();
			}
		}
	}
	cnt-=wt_tmp;
	/*clsb(cs_tmp);*/
	if(cnt==0){ cls(25);printw("\nno living cells.\n"); refresh(); }
	/*endwin();*/
	return(cnt);
};

int disp_occp_grid(WINDOW *win, int dispx, int dispy) { /* displays the occupation grid. */
	int x, y, bg=BLACK;
	wmove(win, dispy, dispx);
	dispx--;
	/*dispy--;*/
	
	textcolor(BOLD, CYAN, BLACK);
	wmove(win, dispy, dispx+2);
	for(x=1;(GRIDSIZEX-1)>x;x++) {
		if(x%2==0){textcolor(BRIGHT, CYAN, BLACK);}
		else{textcolor(BRIGHT, WHITE, BLACK);}
		wprintw(win,"%02d", x);
	}
	wprintw(win,"%c", 187);
	dispy++;
	wmove(win, dispy, dispx);
	for(y=1;(GRIDSIZEY-1)>y;y++) {
		
		if(y%2==0){textcolor(BRIGHT, CYAN, BLACK);}
		else{textcolor(BRIGHT, WHITE, BLACK);}
		wprintw(win,"%d", y%10);
		textcolor(BRIGHT, WHITE, BLACK);
		
		for(x=1;(GRIDSIZEX-1)>x;x++) {
			/*if( x%2==0 ^ y%2==0 ){bg=BLACK;}
			else{bg=CYAN;}*/
			switch(occp[x][y]) {
				case DEAD_CELL: textcolor(BRIGHT, WHITE, BLACK); wprintw(win,"%c%c", 176, 176); break;
				case LIVE_CELL: textcolor(BRIGHT, GREEN, BLACK); wprintw(win,"%c%c", 178, 178); break;
				case WALL_CELL: textcolor(BRIGHT, BLUE, BLACK);  wprintw(win,"%c%c", 177, 177); break;
				default: textcolor(BRIGHT, RED, BLACK); wprintw(win,"%2d", occp[x][y]); break; /* outputs the invalid value */
			}
			
			textcolor(6, 7, 0);
			/*wprintw(win,"  ");*/
		}
		/*wprintw(win,"\n");*/
		wprintw(win,"%c", 186);
		wmove(win, y+dispy, dispx);
	}
	wrefresh(win);
	return 1;
};

int disp_dna_grid() {
	int x, y, dna;
	for(x=0;GRIDSIZEX>x;x++) {
		for(y=0;GRIDSIZEY>y;y++) {
			for(dna=0;DNASIZE>dna;dna++) {
				printw("%d", grid_box[x][y][dna]);
			}
			printw(", ");
		}
		printw("\n\n");
	}
	refresh();
	return 1;
};

int acids(int aa_x, int aa_y, int dna_size){ /* displays the amino acid sequence for given cell */
	int dna_tmp=0;
	
	for(dna_tmp=0;dna_tmp<dna_size;dna_tmp++) {
		     if(grid_box[aa_x][aa_y][dna_tmp]==0 && grid_box[aa_x][aa_y][(dna_tmp+1)]==0 && grid_box[aa_x][aa_y][(dna_tmp+2)]==0)
			{textcolor(0,    7, 0); printf(" 0 "); textcolor(0, 7, 0); }/* 000 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==0 && grid_box[aa_x][aa_y][(dna_tmp+1)]==0 && grid_box[aa_x][aa_y][(dna_tmp+2)]==1)
			{textcolor(BOLD, 7, 1); printf(" 1 "); textcolor(0, 7, 0); }/* 001 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==0 && grid_box[aa_x][aa_y][(dna_tmp+1)]==1 && grid_box[aa_x][aa_y][(dna_tmp+2)]==0)
			{textcolor(BOLD, 0, 2); printf(" 2 "); textcolor(0, 7, 0); }/* 010 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==1 && grid_box[aa_x][aa_y][(dna_tmp+1)]==0 && grid_box[aa_x][aa_y][(dna_tmp+2)]==0)
			{textcolor(BOLD, 7, 3); printf(" 3 "); textcolor(0, 7, 0); }/* 100 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==0 && grid_box[aa_x][aa_y][(dna_tmp+1)]==1 && grid_box[aa_x][aa_y][(dna_tmp+2)]==1)
			{textcolor(BOLD, 7, 4); printf(" 4 "); textcolor(0, 7, 0); }/* 011 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==1 && grid_box[aa_x][aa_y][(dna_tmp+1)]==0 && grid_box[aa_x][aa_y][(dna_tmp+2)]==1)
			{textcolor(BOLD, 7, 5); printf(" 5 "); textcolor(0, 7, 0); }/* 101 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==1 && grid_box[aa_x][aa_y][(dna_tmp+1)]==1 && grid_box[aa_x][aa_y][(dna_tmp+2)]==0)
			{textcolor(BOLD, 0, 6); printf(" 6 "); textcolor(0, 7, 0); }/* 110 */
		else if(grid_box[aa_x][aa_y][dna_tmp]==1 && grid_box[aa_x][aa_y][(dna_tmp+1)]==1 && grid_box[aa_x][aa_y][(dna_tmp+2)]==1)
			{textcolor(BOLD, 0, 7); printf(" 7 "); textcolor(0, 7, 0); }/* 111 */
		
		dna_tmp++;
		dna_tmp++;
	}
	/*printf("\n");*/
	/* cleans up. */
	free(malloc(aa_x));
	free(malloc(aa_y));
	free(malloc(dna_size));
	free(malloc(dna_tmp));
	textcolor(0, 7, 0);
	return 1;
};

void cls(int times) {
	int i=0;
	move(0, 0);
	times*=80;
	for(i=0;i<=times;i++) {
		printw(" ");
	}
	refresh();
	move(0, 0);
};

void clsb(int times) {
	int i=0;
	/*move(0, 0);*/
	times*=80;
	for(i=0;i<=times;i++) {
		printw(" ");
	}
	/*refresh();*/
	/*move(0, 0);*/
};

void wclsb(WINDOW *win, int h, int w) {
	int i=0, times=0;
	wmove(win, 0, 0);
	times=(h-1)*(w-1);
	for(i=0;i<=times;i++) {
		wprintw(win," ");
	}
	/*refresh();*/
	wmove(win, 0, 0);
};

void genstats(void) {
	int bg=BLACK;
	int attr=BOLD;
	attroff(A_BOLD);
	textcolor(attr, 7, bg);
	printw("Generation: ");
	textcolor(attr, BLUE, bg);
	printw("%-10d", loop);
	textcolor(attr, 7, bg);
	printw(" Strongest: ");
	textcolor(attr, RED, bg);
	printw("%-5d",  strongest());
	textcolor(attr, 7, bg);
	printw(" Weakest: ");
	textcolor(attr, MAGENTA, bg);
	printw("%-5d", weakest());
	textcolor(attr, 7, bg);
	printw(" Cells: ");
	textcolor(attr, GREEN, bg);
	printw("%-5d", cell_cnt(GRIDSIZEX, GRIDSIZEY, DNASIZE));
	textcolor(attr, 7, bg);
	attroff(attr);
	refresh();
	
};
